package com.ravi.controller;

import com.ravi.common.utils.jwt.JWTUtils;
import com.ravi.model.dto.DoctorUserDto;
import com.ravi.model.query.DoctorLoginReq;
import com.ravi.model.resp.R;
import com.ravi.model.vo.DoctorUserVo;
import com.ravi.service.intf.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class DoctorController {

	@Autowired
	private DoctorService doctorService;

	@PostMapping("/doc/login")
	public R doctorLogin(@RequestBody DoctorLoginReq doctorLoginReq) {
		DoctorUserDto doctorDto = doctorService.doctorLogin(doctorLoginReq.getUsername(), doctorLoginReq.getPassword());
		Map<String,String> paras = new HashMap<>();
		paras.put("username", doctorDto.getUsername());
		paras.put("roomCode", doctorDto.getRoomCode());
		paras.put("areaCode", doctorDto.getAreaCode());
		String token = JWTUtils.getAccessToken(paras);

		DoctorUserVo vo = new DoctorUserVo();
		vo.setName(doctorDto.getName());
		vo.setRoomName(doctorDto.getRoomName());
		vo.setAreaName(doctorDto.getAreaName());
		vo.setClientName(doctorDto.getClientName());
		R r = R.ok().put("doctor", vo);
		r.put("token", token);
		return r;
	}
}
